/**
 * Created by sicongdu on 17-12-19.
 */

var validate = () => {
    var stNumber = document.getElementById("loginName").value
    var password = document.getElementById("Possword").value

    if(stNumber.length != 10){
        alert("学号格式错误!")
    }else if(password == ""){
        alert("密码不能为空!")
    }else{
        var http_request = new XMLHttpRequest();
        try{
            // Opera 8.0+, Firefox, Chrome, Safari
            http_request = new XMLHttpRequest();
        }catch (e){
            // Internet Explorer Browsers
            try{
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e) {
                try{
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }

        http_request.onreadystatechange  = () => {
            if (http_request.readyState == 4  )
            {
                // Javascript function JSON.parse to parse JSON data
                var jsonObj = JSON.parse(http_request.responseText);

                // jsonObj variable now contains the data structure and can
                // be accessed as jsonObj.name and jsonObj.country.
                var flag = jsonObj.pass
                if(flag == 0){
                    alert("请先注册！")
                }else if(flag == 1){
                    window.location.href = 'http://www.ourspark.org/group_sign';
                }else if(flag == 2){
                    window.location.href = 'http://www.ourspark.org/mark';
                }
            }
        }

        // 如果用的是post方式请求，要在send之前设置HTTP头
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        http_request.open("POST", "http://www.ourspark.org/login", true);

        var data_json = {
            stNumber: document.getElementsByName("stNumber").value,
            password: document.getElementsByName("password").value
        }
        http_request.send(JSON.stringify(data_json));
    }


}
